import { Component, OnInit ,Input } from '@angular/core';
var $=require('jquery');
var base=require('modules/base.js');
function checkver_test() {
	var _this = this,
		c,
		user = _this.reginum,
		reg = /^1[3|4|5|7|8][0-9]{9}$/,
		res = reg.test(user);
	if(user == null || user == '') {
		_this.reginum_hint = "手机号不能为空";
		return;
	} else if(res) {
		_this.reginum_hint = "";
		c = 1;
	} else {
		_this.reginum_hint = "手机号不符合要求";
		return;
	}
	if(c == 1) return true;
};
function check_register(username, phone, code, password, parent) {
  // 注册
  var _this = this;
  var parent = parent || '';
  base.request(base.api(37), {
      username: username,
      phone: phone,
      code: code,
      password: password,
      parent: parent
  }, function (data) {
      alert('注册成功');
      this.ismycountshow = !this.ismycountshow;
      this.isLoginshow = !this.isLoginshow;
      setTimeout(function () {
          $("#login").hide();
      }, 500);
  }, function (data) {
      alert(data.msg);
  });
};
@Component({
  selector: 'app-base-register',
  templateUrl: './base-register.component.html',
  styleUrls: ['./base-register.component.less']
})
export class BaseRegisterComponent implements OnInit {
	@Input() registerp;
	
	fgetc= true;
	rusername= '';
	rusername_hint= '';
	rpassword= '';
	rpassword_hint= '';
	reginum= '';
	reginum_hint='';
	identifying= '';
	identifying_hint= '';
	inviter= '';
	inviter_hint= '';
	rs=1
	
	closeregister() {
		this.registerp.isRegisterShow=!1;
	};
			//发动验证码
	coding() {
		var _this = this;
		if(checkver_test.call(this)) {
			var phone = _this.reginum;
			if(phone == "") {
				_this.reginum_hint = "手机号不能为空";
				return;
			}
			base.request(base.api(38), {
				'phone': phone
			}, function(data) {
				if(_this.fgetc == true) {
					var fun = function fun() {
						i--;
						if(i === 0) {
							$('#get-code').text("获取验证码").css('background-color', "#11bdc5");
							clearInterval(time);
							_this.fgetc = true;
						} else {
							$('#get-code').text(i + "s").css('background-color', "#c2c2c2");
						}
					};

					_this.fgetc = false;
					var i = 60;
					$('#get-code').text(i + 's').css('background-color', "#c2c2c2");
					var time = setInterval(fun, 1000);
				}
			}, function(data) {
				if(data['code']['0'] === '160040'){
					window['alert']('当天短信发送次数超限');
				}else{
//					window['alert'](data.msg, "提示", "确定");
				}
			});
		}
	};
	toAgree() {
//		this.$router.push({
//			path: '/agree'
//		});
		$(".cover-box").toggle().find('.login-action').eq(1).hide();
	};
			// 验证用户名、手机号、验证码、密码
	checkver(e) {
		var _this = this,
			a,
			b,
			c,
			d,
			username = _this.rusername,
			phone = _this.reginum,
			code = _this.identifying,
			password = _this.rpassword,
			invite = _this.inviter || '',
			reg = /^1[3|4|5|7|8][0-9]{9}$/,
			pat1 = /^[0-9]{4}$/,
			pat2 = /^[a-zA-Z0-9]{6,20}$/,
			pat3 = /^[a-zA-Z0-9]{5,20}$/,
			res = reg.test(phone),
			res1 = pat1.test(code),
			res2 = pat2.test(password),
			res3 = pat3.test(username);
		if(username == null || username == '') {
			_this.rusername_hint = '请输入用户名';
			return;
		} else if(res3) {
			_this.rusername_hint = "";
			d = 1;
		} else {
			_this.rusername_hint = '用户名不符合要求';
			return;
		}
		if(password == null || password == '') {
			_this.rpassword_hint = "密码不能为空";
			return;
		} else if(res2) {
			_this.rpassword_hint = "";
			a = 1;
		} else {
			_this.rpassword_hint = "密码不符合要求";
			return;
		}
		if(phone == null || phone == '') {
			_this.reginum_hint = "账户不能为空";
			return;
		} else if(res) {
			_this.reginum_hint = "";
			c = 1;
		} else {
			_this.reginum_hint = "账户不符合要求";
			return;
		}
		if(code == null || code == '') {
			_this.identifying_hint = "验证码不能为空";
			return;
		} else if(res1) {
			_this.identifying_hint = "";
			b = 1;
		} else {
			_this.identifying_hint = "验证码不符合要求";
			return;
		}
		if(a == 1 && b == 1 && c == 1 && d == 1) {
			check_register.call(_this,username, phone, code, password, invite);
		}
	};
			//点击有账号页面跳转到登录页面
	rightLogin(e) {
		$(".logined.login-action").removeClass("display-none");
    $(".register").css("display","none");
    $(".login2").removeClass("active");
    $(".login1").addClass("active");
	};
	
  constructor() { 
  	window['register']=this;
  }
  ngOnInit() {
  }

}
